package com.zlw.rabbitmq.api.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/12/22 22:56
 */
public class Product {


    public static void main(String[] args) throws IOException, TimeoutException {

        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setVirtualHost("/");
        connectionFactory.setHost("39.100.250.122");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");


        //2. 通过连接工厂 创建连接。
        Connection connection = connectionFactory.newConnection();

        //3. 通过connection 创建channel
        Channel channel = connection.createChannel();
        channel.confirmSelect();
        String exchangeName = "test_confirm_exchange";
        String routingKey = "test.confirm";


        //4. 消息发送
        for (int i = 0; i < 9; i++) {
            String msg = "你好 RabbitMQ ! 测试消息确实机制  --> " + i;
            channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());
        }

        //6. 添加监听
        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.out.println(" ack handleAck " + deliveryTag);
            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.out.println(" no ack  handleNack " + deliveryTag);

            }
        });


    }


}
